package com.wlc.io;

import java.io.*;

/**
 * @author 王立朝 wanglichao@champion-credit.com
 * @description 数据输入流和数据输出流
 * @date 2021-07-10 16:17
 */
public class DateStreamDemo {

    private static final String FILE_PATH = "/Users/wanglichao/Desktop/temp/b.txt";
    private static final String FILE_PATH2 = "/Users/wanglichao/Desktop/temp/d.txt";

    /**
     * 数据输入流DataInputStream  读取文件
     */
    public static void dataInputStream() {
        try (FileInputStream fileInputStream = new FileInputStream(FILE_PATH);
             DataInputStream dataInputStream = new DataInputStream(fileInputStream)) {
            System.out.println("dataInputStream.readInt() = " + dataInputStream.readInt());
            System.out.println("dataInputStream.readBoolean() = " + dataInputStream.readBoolean());
            System.out.println("dataInputStream.readChar() = " + dataInputStream.readChar());
            System.out.println("dataInputStream.readUTF() = " + dataInputStream.readUTF());

            System.out.println("读取完毕");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 数据输出流 DataOutputStream
     */
    public static void dataOutputStream() {
        try (FileOutputStream fileOutputStream = new FileOutputStream(FILE_PATH);
             DataOutputStream dataOutputStream = new DataOutputStream(fileOutputStream)) {
            dataOutputStream.writeInt(5);
            dataOutputStream.writeBoolean(false);
            dataOutputStream.writeChar(33);
            dataOutputStream.writeUTF("我是一个人,hello world");
            System.out.println("写入完毕");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 使用缓存流 输出 2个数字，并用缓存流读取；
    // 使用数据流DataInputStream
    public static void method01(){
        try(FileInputStream fileInputStream = new FileInputStream(FILE_PATH2);
        FileOutputStream fileOutputStream = new FileOutputStream(FILE_PATH2);
        DataInputStream dataInputStream = new DataInputStream(fileInputStream);
        DataOutputStream dataOutputStream = new DataOutputStream(fileOutputStream)){
              dataOutputStream.writeInt(1);
              dataOutputStream.writeInt(2);
            System.out.println("写入完毕");
            System.out.println("dataInputStream.readInt() = " + dataInputStream.readInt());
            System.out.println("dataInputStream.readInt() = " + dataInputStream.readInt());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void method02(){
        try(FileWriter fileWriter = new FileWriter(FILE_PATH2);
        PrintWriter printWriter = new PrintWriter(fileWriter);
        FileReader fileReader = new FileReader(FILE_PATH2);
        BufferedReader bufferedReader = new BufferedReader(fileReader)){
            printWriter.print(1);
            printWriter.print(2);
            printWriter.flush();
            System.out.println("缓存流写入完毕");
           while (true){
               String line = bufferedReader.readLine();
               if(null == line){
                   break;
               }
               System.out.println("缓存读取到的是"+ line);
           }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        method01();
        method02();
    }
}
